package com.asianrapid.controller.sys;


import com.asianrapid.model.sys.CarsEntity;
import com.asianrapid.model.sys.PageResult;
import com.asianrapid.service.sys.CarsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CarsController {
    private Logger log = LoggerFactory.getLogger(CarsController.class);

    @Autowired
    private CarsService carsService;

    @GetMapping("/carAll")
    public PageResult getAllCar(int pageSize, int page){

        PageResult pageResult = new PageResult();
        pageResult.setData(carsService.carsList( pageSize,page * pageSize ));
        pageResult.setTotalCount(carsService.carsSize( pageSize,page * pageSize ));
        log.debug("The method is ending");

        return pageResult;

    }/*menus/cars*/

    @PostMapping("/menus/cars")
    public void insertCars(@RequestBody CarsEntity carsEntity ){
        carsService.insertCars(carsEntity);
    }/*/menus/carss*/


    @GetMapping("/menus/carss")
    public PageResult getCarType(){
        PageResult pageResult = new PageResult();
        pageResult.setData(carsService.getCarType());
        return pageResult;
    }


    @PutMapping("/cars/{id}")
    public CarsEntity updateCars(@RequestBody CarsEntity carsEntity, @PathVariable int id) {
        if (carsEntity.getId() == id) {
            carsService.updateCars(carsEntity);
        }
        log.debug("The method is ending");
        return carsEntity;
    }


    @DeleteMapping("/DelCars")
    public List<String> deleteCars(@RequestBody List<String> groupId) {
        carsService.deleteCars(groupId);
        return groupId;
    }/*/zgr/cars*/


    @PostMapping("/zgr/cars")
    public CarsEntity getCars(@RequestBody List<Integer> groupId){

        return carsService.getCars(groupId.get(0));

    }

}
